package com.charlotte.gupao.study.springbootrabbitmqdemo.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author junjie.ding
 * @date 2020/10/29 22:28
 * @description TODO
 */
@Configuration
public class RabbitmqConfig {


    @Bean("topicExchange")
    public TopicExchange getTopicExchange(){
        return new TopicExchange("TOPIC_EXCHANGE");
    }

    @Bean("fanoutExchange")
    public FanoutExchange getFanoutExchange(){
        return new FanoutExchange("FANOUT_EXCHANGE");
    }


    @Bean("directQueue")
    public Queue directQueue(){
        // durable:是否持久化,默认是false,持久化队列：会被存储在磁盘上，当消息代理重启时仍然存在，暂存队列：当前连接有效
        // exclusive:默认也是false，只能被当前创建的连接使用，而且当连接关闭后队列即被删除。此参考优先级高于durable
        // autoDelete:是否自动删除，当没有生产者或者消费者使用此队列，该队列会自动删除。
        //   return new Queue("TestDirectQueue",true,true,false);

        //一般设置一下队列的持久化就好,其余两个就是默认false
        return new Queue("DIRECT_QUEUE_TEST",true);
    }

    @Bean("topicQueue")
    public Queue topicQueue(){
        return new Queue("TOPIC_QUEUE_TEST");
    }

    @Bean("fanoutQueue1")
    public Queue fanoutQueue1(){
        return new Queue("FANOUT_QUEUE_TEST_1");
    }
    @Bean("fanoutQueue2")
    public Queue fanoutQueue2(){
        return new Queue("FANOUT_QUEUE_TEST_2");
    }

    @Bean
    public Binding bindingTopic(@Qualifier("topicQueue") Queue queue, @Qualifier("topicExchange") TopicExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("#.djj.#");
    }

    @Bean
    public Binding bindingFanout1(@Qualifier("fanoutQueue1") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }
    @Bean
    public Binding bindingFanout2(@Qualifier("fanoutQueue2") Queue queue, @Qualifier("fanoutExchange") FanoutExchange exchange){
        return BindingBuilder.bind(queue).to(exchange);
    }


}
